package com.hegongda.servlet.filter;

import com.hegongda.bean.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@WebFilter(urlPatterns = "/view/admin/*",dispatcherTypes = {DispatcherType.REQUEST,DispatcherType.FORWARD})
public class AdminFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;
        User user = (User)request.getSession().getAttribute("user");
        if(user != null) {
            if (user.getCode() == 1) {
                chain.doFilter(request, response);
            } else {
                response.sendRedirect("/Store/index.jsp");
            }
        }else {
            response.sendRedirect("/Store/index.jsp");
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
